
/**********************************
DESCRIPTION: This file appends the Medicare claims data collected in the previous programs and merges on 
beneficiary demographic information. it also constructs the health information
from the claims data

The program uses the following datasets:
 ( 1-8 ) bene_icdpre_bcarrier_claims2005-bene_icdpre_bcarrier_claims2012
 ( 9-16) bene_icdpre_medpar_all_file_2005-bene_icdpre_medpar_all_file_2012 
 (17-25) bene_icdpre_outpatient_base_claims_2005-bene_icdpre_outpatient_base_claims_2012 
 (26-34) 2005/mbsf_cc_summary.dta - 2012/mbsf_cc_summary.dta
 (34-42) 2005/mbsf_ab_summary.dta - 2012/mbsf_ab_summary.dta 
 (43-51) 2005/mbsf_costuse.dta - 2012/mbsf_costuse.dta  

The program creates the following datasets:
  ( 1-8 ) bene_icd_all_2005 - bene_icd_all_2012 
  ( 9 )   bene_hlth1_all.dta 
  ( 10)   bene_hlth2_all.dta
  (11-19) bene_icdDeath_all_2005 -bene_icdDeath_all_2012
  

**********************************/

set more off
capture log close
clear all

global dataIn "N:\MedicareClaims-P045601-BE\Work\hosp_retro\health_out\Data-In\"
global dataOut "N:\MedicareClaims-P045601-BE\Work\hosp_retro\health_out\Data-Out\"
global dpath "N:\MedicareClaims-P045601-BE\Work\ay_data"
global cpath "N:\MedicareClaims-P045601-BE\Work"

adopath +  N:\SIL-Common\estout
adopath +  N:\SIL-Common\outreg2
adopath +  N:\SIL-Common\reghdfe-master\package

*Append all of the claims-level datasets
foreach yr of numlist 2005(1)2012 {
  use "$dataOut/bene_icdpre_bcarrier_claims_`yr'.dta"
  append using "$dataOut/bene_icdpre_medpar_all_file_`yr'.dta"
  append using "$dataOut/bene_icdpre_outpatient_base_claims_`yr'.dta"

 *SPECIFIC HEALTH CONDITIONS 
  *acute myocardial infarction
  gen icd_ami = icd3_num == 410
  *ischemic heard disease
  gen icd_ihd = inlist(icd3_num, 411 , 414)
  *glaucoma
  gen icd_glaucoma = inlist(icd3_num, 365)
  *diabetes complications 1
  gen diab_compl1=diab_compl==1
  *diabetes complications 2
  gen diab_compl2=diab_compl==2
 
 keep icd* malignant* benign* unspecified* primary_hyp* compl_hyp* secondary_hyp* hypertension* diab* typeii* chronic* BENE date
 drop icd_5 icd_4 icd_3
 collapse (max) icd* malignant* benign* unspecified* primary_hyp* compl_hyp* secondary_hyp* hypertension* diab* typeii* chronic* , by(BENE date)
 
save "$dataOut/bene_icd_all_`yr'.dta", replace
clear

}



*Medicare conditions from the beneficiary summary files
foreach yr of numlist 2005(1)2012 {
 di "`yr'"
 if `yr' == 2005 {
 di "`yr'"
  use "$cpath/`yr'/mbsf_cc_summary.dta" 
  }
 else {
 di "`yr' blah"
 append using "$cpath/`yr'/mbsf_cc_summary.dta"  
  }
}
 rename BENE_ENROLLMT_REF_YR year
 keep BENE_ID year ami AMI* cataract CATARACT* chf CHF* diabetes DIABETES* glaucoma GLAUCOMA* ischemicheart ISCHEMICHEART* STROKE*  
 save  "$dataIn/bene_hlth1_all.dta" , replace
clear

*Medicare death dates
foreach yr of numlist 2005(1)2012 {
 di "`yr'"
 if `yr' == 2005 {
 di "`yr'"
  use "$cpath/`yr'/mbsf_ab_summary.dta" 
  }
 else {
 di "`yr' blah"
 append using "$cpath/`yr'/mbsf_ab_summary.dta"  
  }
}
 rename BENE_ENROLLMT_REF_YR year
 keep BENE_ID year BENE_DEATH_DT NDI_DEATH_DT  
 sort BENE_ID year 

 save  "$dataIn/bene_hlth2_all.dta" , replace
clear

*Medicare readmissions 
foreach yr of numlist 2005(1)2012 {
 di "`yr'"
 if `yr' == 2005 {
 di "`yr'"
  use "$cpath/`yr'/mbsf_costuse.dta" 
  }
 else {
 di "`yr' blah"
 append using "$cpath/`yr'/mbsf_costuse.dta"  
  }
}

 rename BENE_ENROLLMT_REF_YR year
 keep BENE_ID year readmissions 
 sort BENE_ID year 
 replace readmissions =0 if readmissions ==.
 collapse (sum) readmissions, by(BENE_ID year)
 save  "$dataIn/bene_hlth3_all.dta" , replace
clear

use "$dataIn/bene_hlth3_all.dta"  

*Put the Medicare conditions, death dates, and readmissions together
merge 1:1 BENE_ID year using "$dataIn/bene_hlth2_all.dta"
drop _merge

sort BENE_ID year 
merge 1:1 BENE_ID year using "$dataIn/bene_hlth1_all.dta"
drop _merge
rename diabetes DIABETES
save  "$dataIn/bene_allhlth_allyrs.dta" , replace
clear


*Put the Medicare health conditions, etc, onto the ICD dataset

foreach yr of numlist 2005(1)2012 {
  use "$dataOut/bene_icd_all_`yr'.dta" 
  gen year=year(date)
  sort BENE_ID
  merge m:1 BENE_ID year using "$dataIn/bene_allhlth_allyrs.dta" 
  drop if _merge==2
  drop _merge
 save  "$dataOut/bene_icdDeath_all_`yr'.dta" , replace
	}


 
